package paperfly.game.handler;
 
import paperfly.game.Player;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyInputAction;
import com.jme.input.action.KeyNodeLookDownAction;
import com.jme.input.action.KeyNodeLookUpAction;
import com.jme.input.action.KeyNodeRotateLeftAction;
import com.jme.input.action.KeyNodeRotateRightAction;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
 
/**
 * Input Handler for the Flag Rush game. This controls a supplied spatial
 * allowing us to move it forward, backward and rotate it left and right.
 * @author Mark Powell
 *
 */
public class EnterGameHandler extends InputHandler {
 
    public static final String EMBICAR = "embicar";
	public static final String EMPINAR = "empinar";
	public static final String LIGAR_MOTORES = "ligar";
	public static final String ROTACIONAR_ESQUERDA = "rotacionarEsquerda";
	public static final String ROTACIONAR_DIREITA = "rotacionarDireita";
	private static final String NIVELAR = "nivelar";
	private Player player;
	private KeyNodeRotateRightAction rotacionarEsquerda;
	private KeyNodeRotateLeftAction rotacionarDireita;
	private KeyNodeLookDownAction embicarAction;
	private KeyNodeLookUpAction empinarAction;
	private boolean canRotate;
	/**
     * Supply the node to control and the api that will handle input creation.
     * @param node the node we wish to move
     * @param api the library that will handle creation of the input.
     */
    public EnterGameHandler(Player node) {
        this.player = node;
    	setKeyBindings();
        setActions();
        this.canRotate = true;
        ativarRotacoes();
    }
 
    /**
     * creates the keyboard object, allowing us to obtain the values of a keyboard as keys are
     * pressed. It then sets the actions to be triggered based on if certain keys are pressed (WSAD).
     * @param api
     */
    private void setKeyBindings() {
        KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
        
        keyboard.set(LIGAR_MOTORES, KeyInput.KEY_0);
        keyboard.set(EMBICAR, KeyInput.KEY_W);
        keyboard.set(EMPINAR, KeyInput.KEY_S);
        keyboard.set(ROTACIONAR_DIREITA, KeyInput.KEY_A);
        keyboard.set(ROTACIONAR_ESQUERDA, KeyInput.KEY_D);
        keyboard.set(NIVELAR, KeyInput.KEY_N);
        
    }
 
    /**
     * assigns action classes to triggers. These actions handle moving the node forward, backward and 
     * rotating it.
     * @param player the node to control.
     */
    private void setActions() {
    	Vector3f eixoZ = player.getLocalRotation().getRotationColumn(2);

    	KeyInputAction start = new KeyInputAction(){
			public void performAction(InputActionEvent evt) {
				if(!player.vehicleIsMoving()){
					player.setAcceleration(2);
					player.setVelocity(1);
					player.setMaxSpeed(100);
					Thread controle = new Thread(new Runnable() {
						public void run() {
							try {
								Thread.sleep(10000);
							} catch (InterruptedException e) {}
							addAction(empinarAction, EMPINAR, true);
						}
					});
					controle.start();
				}
			}
		};
        addAction(start, LIGAR_MOTORES, false);
        InputAction nivelarAction = new InputAction() {
			public void performAction(InputActionEvent evt) {
				Thread anim = new Thread(new Runnable() {
					public void run() {
						float angle = 0.1f;
						while(angle < 2.1 ){
							player.getLocalRotation().fromAngleAxis(angle * FastMath.PI, new Vector3f(0,0,1));
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {}
							angle += 0.1f;
						}
					}
				});
				anim.start();
			}
		};
		addAction(nivelarAction, NIVELAR,true);
        rotacionarEsquerda = new KeyNodeRotateRightAction(player, 1f);
		rotacionarEsquerda.setLockAxis(eixoZ);
        
        rotacionarDireita = new KeyNodeRotateLeftAction(player, 1f);
        rotacionarDireita.setLockAxis(eixoZ);

        embicarAction = new KeyNodeLookDownAction(player, 1f);
        empinarAction = new KeyNodeLookUpAction(player, 1f);
        
    }
    public void ativarRotacoes(){
		if(canRotate){
	    	addAction(rotacionarEsquerda, ROTACIONAR_DIREITA, true);
			addAction(rotacionarDireita, ROTACIONAR_ESQUERDA, true);
			addAction(embicarAction, EMBICAR, true);
			canRotate = false;
		}
    }
}